package icu.groot.alan.dao.impl;

import icu.groot.alan.dao.TaskDao;
import icu.groot.alan.entity.Task;
import icu.groot.alan.service.TaskService;
import icu.groot.alan.util.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TaskDaoImpl implements TaskDao {
    @Override
    public List<Task> getAllTask(Connection connection) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Task> tasks = new ArrayList<>();
        Task task = null;
        String sql = "select * from task";
        Object[] params = {};
        resultSet = Dao.execute(connection, preparedStatement, resultSet, sql, params);
        while (resultSet.next()) {
            task = new Task();
            task.setTaskName(resultSet.getString("task_name"));
            task.setTaskCode(resultSet.getInt("task_code"));
            tasks.add(task);
        }
        Dao.closeResource(preparedStatement, resultSet);
        return tasks;
    }
}
